﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
namespace AbingtonBooks
{
    public class EmailClient
    {
         public static void SendEmail(Customer customer,Order order)
    {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(customer.Email);
            mail.From = new System.Net.Mail.MailAddress("AbingtonBooks@gmail.com");
            mail.Subject = "Order Receipt";

            string Body = "Thank you for your Order!" + "<br/>" + "Order ID: "+ order.OrderId + "<br/>" + "Tracking Number: " + order.Tracking + "<br/>";
            using (dbAbingtonBooks context = new dbAbingtonBooks()){
            List<OrderItem> orderItems = context.OrderItems.Where(o => o.OrderId == order.OrderId).ToList();
                decimal total = 0;
            foreach(OrderItem item in orderItems){
            Book book = context.Books.Where(b => b.BookId == item.BookId).First();
            Body += "Item : " + book.Title + " Price: $" + book.Price + "<br/>";
                total += book.Price;
            }
                Body+= "Total: $" + total.ToString();
            }
             
             mail.Body = Body;

            mail.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "smtp.gmail.com";

            smtp.Credentials = new System.Net.NetworkCredential("AbingtonBooks@gmail.com", "abingtonbooks421");
            smtp.EnableSsl = true;
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(mail);

}
    }
}